//
//  GJokeTextTVC.m
//  guess
//
//  Created by fank on 15/11/6.
//  Copyright © 2015年 fank. All rights reserved.
//

#import "GJokeTextTVC.h"
#import "PhotoModel.h"
#import "PhotoItemView.h"
#import "CoreSVP.h"



@interface GJokeTextTVC()
{
JokeSingleTextDataModel * _singleData;
JokeSingleImageDataModel * _singleImageData;
 UIView *  _allView;
 UIView  * _topView;
 UILabel * _titleLab;
 UILabel * _timeLab;

 UILabel * _textLab;

 UIView  * _bottomView;
 UIButton *	_transmitBtn;
 UIButton *  _saveBtn;
 CGRect _frame;
 JokeType _jokeType;
}
@property  UIImage * myImage;

@end



@implementation GJokeTextTVC

-(instancetype)initWithTextData:(JokeSingleTextDataModel  *)singleData
{
    self.userInteractionEnabled = YES;
    self.backgroundColor =[UIColor clearColor];
    _singleData =singleData;
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    
    _topView=[[UIView alloc]init];
    _titleLab=[[UILabel alloc]init];
    _timeLab=[[UILabel alloc]init];
    
    _textLab=[[UILabel alloc]init];
    
    _bottomView=[[UIView alloc]init];
    _transmitBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    _allView =[[UIView alloc]init];
    _allView.userInteractionEnabled = YES;
    _jokeType = JokeText;
    
    [self addSubview:_allView];
    return self;
}


-(instancetype)initWithImageData:(JokeSingleImageDataModel  *)singleData
{
    
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    self.userInteractionEnabled = YES;
    
    _singleImageData =singleData;
    
    _topView=[[UIView alloc]init];
    _titleLab=[[UILabel alloc]init];
    _timeLab=[[UILabel alloc]init];
    
    _imageImg=[[UIImageView alloc]init];
    _saveBtn =[UIButton buttonWithType:UIButtonTypeCustom];

    _bottomView=[[UIView alloc]init];
    _transmitBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    _allView =[[UIView alloc]init];
    _allView.userInteractionEnabled =YES;
    
    [self addSubview:_allView];
    _jokeType = JokeImage;
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    return self;
}

-(void)setSpitslotImage:(UIImage *)img
{
    _myImage =img;
}

+(CGSize)heightWithImage:(UIImage *)img
{
    return img.size;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    if (_jokeType == JokeText) {
        
    CGSize labelSize = {0, 0};
    labelSize = [_singleData.text sizeWithFont:[UIFont systemFontOfSize:17]
                             constrainedToSize:CGSizeMake(kscreenWith -20 , 5000)
                                 lineBreakMode:NSLineBreakByWordWrapping];
    
    
    _allView.frame = CGRectMake(0, 0,kscreenWith-20 , self.frame.size.height-10);
    
    _allView.layer.cornerRadius =10;
    _allView.backgroundColor =[UIColor whiteColor];
    
    _topView.frame = CGRectMake(0, 0,(kscreenWith-20) , 30);
    _topView.backgroundColor =UIColorFromRGBValue(0xF6F6F6);
    _topView.layer.cornerRadius =10;
    [_allView addSubview:_topView];
    
    
    _titleLab.frame =CGRectMake(5, 5, kscreenWith-190, 25);
    _titleLab.font =[UIFont fontWithName:@"" size:12];
    _titleLab.text =_singleData.title;
    [_topView addSubview:_titleLab];
    
    _timeLab.frame =CGRectMake(kscreenWith-180, 5, 170, 25);
    _timeLab.font =[UIFont fontWithName:@"" size:12];
    _timeLab.text =[_singleData.ct substringToIndex:19];
    [_topView addSubview:_timeLab];
    
    
    

    
    
    _textLab.frame =CGRectMake(0, 31, kscreenWith -20 , labelSize.height+10);
    _textLab.numberOfLines = 0;
    _textLab.text = _singleData.text;
    _textLab.backgroundColor =[UIColor whiteColor];
    [_allView addSubview:_textLab];
    
    
   
    _bottomView.frame =CGRectMake(0, labelSize.height+41, kscreenWith-20, 40);
    _bottomView.backgroundColor = UIColorFromRGBValue(0xF6F6F6);
    _bottomView.userInteractionEnabled =YES;
    [_allView addSubview:_bottomView];
    
    _transmitBtn.frame =CGRectMake(kscreenWith/2 -40, 5, 40, 30);
    [_transmitBtn setTitle:@"分享" forState:UIControlStateNormal];
    [_transmitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_transmitBtn addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchDown];
    [_bottomView addSubview:_transmitBtn];
        
    
        
    }else if(_jokeType == JokeImage&&_singleImageData!=nil){
        
        
        _allView.frame = CGRectMake(0, 0,kscreenWith-20 , self.frame.size.height-15);
        _allView.layer.cornerRadius =10;
        _allView.backgroundColor =[UIColor whiteColor];
        
        _topView.frame = CGRectMake(0, 0,(kscreenWith-20) , 30);
        _topView.backgroundColor =UIColorFromRGBValue(0xF6F6F6);
        _topView.layer.cornerRadius =10;
        [_allView addSubview:_topView];
        
        
        _titleLab.frame =CGRectMake(5, 5, kscreenWith-190, 25);
        _titleLab.font =[UIFont fontWithName:@"" size:12];
        _titleLab.textColor =[UIColor blackColor];
        _titleLab.text =_singleImageData.title;
        [_topView addSubview:_titleLab];
        
        _timeLab.frame =CGRectMake(kscreenWith-180, 5, 170, 25);
        _timeLab.font =[UIFont fontWithName:@"" size:12];
        _timeLab.text =[_singleImageData.ct substringToIndex:19];
        _timeLab.textColor =[UIColor blackColor];
        [_topView addSubview:_timeLab];
        
        
        
        
        
        
        _imageImg.frame =CGRectMake(0, 31, kscreenWith -20 , _myImage.size.height/_myImage.size.width*(kscreenWith-20));
        _imageImg.backgroundColor =[UIColor whiteColor];
        [_allView addSubview:_imageImg];

        _imageImg.image =_myImage;

        
        
        //开启事件
        _imageImg.userInteractionEnabled = YES;
        
        //模式
        _imageImg.contentMode=UIViewContentModeScaleAspectFill;
        
        _imageImg.clipsToBounds = YES;
        
        //添加手势
        [_imageImg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchImage:)]];
        
        
        
        _bottomView.frame =CGRectMake(0, _myImage.size.height/_myImage.size.width*(kscreenWith-20)+32, kscreenWith-20, 43);
        _bottomView.userInteractionEnabled = YES;
        _bottomView.backgroundColor = [UIColor lightGrayColor];;
        [_allView addSubview:_bottomView];
        
        _transmitBtn.frame =CGRectMake(0, 1, kscreenWith/2-10-0.5, 39);
        _transmitBtn.backgroundColor =UIColorFromRGBValue(0xF6F6F6);
        [_transmitBtn setTitle:@"分享" forState:UIControlStateNormal];
        _transmitBtn.userInteractionEnabled = YES;
        [_transmitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_transmitBtn addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchDown];
        [_bottomView addSubview:_transmitBtn];
        
        
        _saveBtn.frame =CGRectMake(kscreenWith/2-10+0.5, 1, kscreenWith/2-10-0.5, 39);
        _saveBtn.backgroundColor =UIColorFromRGBValue(0xF6F6F6);
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        _saveBtn.userInteractionEnabled = YES;
        [_saveBtn addTarget:self action:@selector(savePhoto) forControlEvents:UIControlEventTouchDown];
//        [_saveBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(savePhoto)]];
        [_saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_bottomView addSubview:_saveBtn];
        
    }
    
        
    
}



-(void)touchImage:(UITapGestureRecognizer *)tap{
    if(_ClickImageBlock != nil) _ClickImageBlock(_singleImageData);
}


#pragma mark - save photo
- (void)savePhoto{
    

    
    PhotoModel *pbModel=[[PhotoModel alloc] init];
    pbModel.mid = (NSUInteger )self.index+1;
    
    pbModel.title = [NSString stringWithFormat:@"%@",[_singleImageData.ct substringFromIndex:18]];
    pbModel.desc = [NSString stringWithFormat:@"%@",_singleImageData.title];
    pbModel.image_HD_U = _singleImageData.img;
    
    //取出对应模型
    PhotoModel *itemModel = (PhotoModel *)pbModel;
    
    //取出itemView
    PhotoItemView * itemImage = [[PhotoItemView alloc]init];
    itemImage.photoModel =itemModel;
    
    if(!itemImage.hasImage){
        [CoreSVP showSVPWithType:CoreSVPTypeError Msg:@"无图片数据" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
        return;
    }
    
    //读取缓存
    if([itemModel read]){//已经保存过本地
        
        [CoreSVP showSVPWithType:CoreSVPTypeInfo Msg:@"图片已存" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
    }else{
        
        //展示提示框
        [CoreSVP showSVPWithType:CoreSVPTypeLoadingInterface Msg:@"保存中" duration:0 allowEdit:NO beginBlock:nil completeBlock:nil];
        
        [itemImage save:^{
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [CoreSVP showSVPWithType:CoreSVPTypeSuccess Msg:@"保存成功" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
                
                //保存记录
                [itemModel save];
            });
            
            
        } failBlock:^{
            
            [CoreSVP showSVPWithType:CoreSVPTypeError Msg:@"保存失败" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
        }];
    }
}

-(void)shareBtnClick
{
    NSLog(@"11111111分享");
     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (_jokeType == 1) {
        [dict setObject:_singleData.title forKey:@"title"];
        [dict setObject:_singleData.text forKey:@"text"];
        [dict setObject:@(_jokeType) forKey:@"jokeType"];
    }else{
        [dict setObject:_singleImageData.title forKey:@"title"];
        [dict setObject:_imageImg.image forKey:@"image"];
        [dict setObject:@(_jokeType) forKey:@"jokeType"];
    }

    [self shareFunWithShareInfo1:dict];
   
}


//分享方法
-(void)shareFunWithShareInfo1:(NSDictionary *)dictionary
{
    BOOL isInstallWeChat = [WXApi isWXAppInstalled];
    BOOL isInstallQQ    = [QQApiInterface isQQInstalled];
    
    NSMutableArray *array = [NSMutableArray array];
    if (isInstallWeChat == YES) {
        [array addObject:UMShareToWechatFavorite];
        [array addObject:UMShareToWechatSession];
        [array addObject:UMShareToWechatTimeline];
    }
    if (isInstallQQ == YES) {
        [array addObject:UMShareToQQ];
    }
    if ([[dictionary objectForKey:@"jokeType"] integerValue] == 2) {
        [array addObject:UMShareToSina];
    }
    if (dictionary) {
        if (array.count == 0) {
            [ABTools showToastView:@"手机中未安装可分享的平台" onView:self.selfVC.view];
        }else if(array.count > 0){
            if ([[dictionary objectForKey:@"jokeType"] integerValue] == 1) {
                NSString *title = [dictionary objectForKey:@"text"];
                [UMSocialSnsService presentSnsIconSheetView:self.selfVC appKey:UmengAppkey shareText:title shareImage:nil shareToSnsNames:array delegate:self.selfVC];
            }else if ([[dictionary objectForKey:@"jokeType"] integerValue] == 2){
                UIImage *image = [dictionary objectForKey:@"image"];
                NSString *title = [dictionary objectForKey:@"title"];
                //微信收藏
                [UMSocialData defaultData].extConfig.wechatFavoriteData.shareImage = image;
                [UMSocialData defaultData].extConfig.wechatFavoriteData.title = title;
                [UMSocialData defaultData].extConfig.wechatFavoriteData.wxMessageType = UMSocialWXMessageTypeImage;
                //微信好友
                [UMSocialData defaultData].extConfig.wechatSessionData.shareImage = image;
                [UMSocialData defaultData].extConfig.wechatSessionData.title = title;
                [UMSocialData defaultData].extConfig.wechatSessionData.wxMessageType = UMSocialWXMessageTypeImage;
                //微信朋友圈
                [UMSocialData defaultData].extConfig.wechatTimelineData.shareImage = image;
                [UMSocialData defaultData].extConfig.wechatTimelineData.title = title;
                [UMSocialData defaultData].extConfig.wechatTimelineData.wxMessageType = UMSocialWXMessageTypeImage;
                //新浪微博
                [UMSocialData defaultData].extConfig.sinaData.shareImage = image;
                //QQ
                [UMSocialData defaultData].extConfig.qqData.shareImage = image;
                [UMSocialData defaultData].extConfig.qqData.title = title;
                [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeImage;
                //QQ空间
                //                [UMSocialData defaultData].extConfig.qzoneData.shareImage = image;
                //                [UMSocialData defaultData].extConfig.qzoneData.title = title;
                
                [UMSocialSnsService presentSnsIconSheetView:self.GjokeVC appKey:UmengAppkey shareText:title shareImage:nil shareToSnsNames:array delegate:self.selfVC];
            }

        }
    }
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
